import sensor, image, lcd, time
from hiwonder import hw_key, hw_uart, hw_led , fill_light
#import cv2

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 100)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)

lcd.init()
led = hw_led()
f_led = fill_light()
serial = hw_uart()
key = hw_key()
f_led.fill_onoff(1)

img_path1 = "image_1.jpg"
img_path2 = "image_2.jpg"
img_num = 0
while True:
    state1 = key.key_scan(1)
    state2 = key.key_scan(2)
    img = sensor.snapshot()
    if (state1 == 1) and (state2 == 0):#显示图像，等待按键按下
        print("press k1")
        img_num = img_num + 1
        #img_path1 = "./picture/red/%s.jpg" %str(img_num)
        #img_path1 = "./picture/blue/%s.jpg" %str(img_num)
        img_path1 = "./picture/green/%s.jpg" %str(img_num)
        print(img_path1)
        img.save(img_path1)
        time.sleep(0.1)
    elif (state1 == 0) and (state2 == 1):
        print("press k2")
        img_num = img_num + 1
        #img_path2 = "./picture/red/%s.jpg" %str(img_num)
        #img_path2 = "./picture/blue/%s.jpg" %str(img_num)
        img_path2 = "./picture/green/%s.jpg" %str(img_num)
        print(img_path2)
        img.save(img_path2)
        time.sleep(0.1)
    elif (state1 == 1) and (state2 == 1):
        #img.binary([rgb565_thres])
        #img.save(img_path1)
        lcd.display(img)
    else:
        lcd.display(img)
